package cn.tedu.xxmall.web;

import cn.tedu.xxmall.ex.ServiceException;
import lombok.Data;

import java.io.Serializable;
@Data
public class JsonResult<T> implements Serializable {
    /**
     * 业务状态
     */
    private Integer state;
    /**
     * 信息
     */
    private String message;
    /**
     * 数据
     */
    private T   data;

    public static JsonResult ok(){
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult JsonResult = new JsonResult();
        JsonResult.setState(ServiceCode.OK);
        JsonResult.setData(data);
        return JsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getState(),e.getMessage());
    }

    public static JsonResult<Void> fail(Integer state ,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(state);
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
